package com.zz.inventory.feign;

import com.ruoyi.common.core.web.domain.AjaxResult;
import lombok.extern.log4j.Log4j2;
import org.example.dto.SchoolPackageRecordDTO;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.net.ConnectException;
import java.util.concurrent.TimeoutException;

@Component
@Log4j2
public class InventoryServiceFallbackFactory implements FallbackFactory<InventoryService> {
    @Override
    public InventoryService create(Throwable cause) {// 当远程调用失败时，会触发该方法（cause为失败原因）
        log.error("SchoolManage服务调用失败，原因: {}", cause.getMessage(), cause);// 记录错误日志（包含异常详情）

        return new InventoryService() {      // 返回InventoryService的降级实现
            @Override
            public ResponseEntity<AjaxResult> add(SchoolPackageRecordDTO dto) {
                // 远程调用失败时，直接抛异常（不返回成功响应），确保异常能传递到服务层触发回滚
                throw new RuntimeException("SchoolManage服务调用失败: " + cause.getMessage(), cause);
            }
        };
    }
}